{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "搜索 - Microsoft 必应\n"
     ]
    }
   ],
   "source": [
    "# 指定你想要获取标题的网站\n",
    "url = 'https://cn.bing.com/' # 抓取bing搜索引擎的网页内容\n",
    "\n",
    "# 发送HTTP请求获取网页内容\n",
    "response = requests.get(url)\n",
    "# 中文乱码问题\n",
    "response.encoding = 'utf-8' \n",
    "# 确保请求成功\n",
    "if response.status_code == 200:\n",
    "    # 使用BeautifulSoup解析网页内容\n",
    "    soup = BeautifulSoup(response.text, 'lxml')\n",
    "    \n",
    "    # 查找<title>标签\n",
    "    title_tag = soup.find('title')\n",
    "    \n",
    "    # 打印标题文本\n",
    "    if title_tag:\n",
    "        print(title_tag.get_text())\n",
    "    else:\n",
    "        print(\"未找到<title>标签\")\n",
    "else:\n",
    "    print(\"请求失败，状态码：\", response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "basicenv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
